// components/choosePhoto/index.js
Component({
    /**
     * 组件的属性列表
     */
    properties: {

    },

    /**
     * 组件的初始数据
     */
    data: {
        photo_src: []
    },

    /**
     * 组件的方法列表
     */
    methods: {
        chooseImage(e) {
            let _this = this;
            wx.chooseImage({
                sizeType: ['original', 'compressed'], //可选择原图或压缩后的图片
                sourceType: ['album', 'camera'], //可选择性开放访问相册、相机
                success: res => {
                    const images = _this.data.photo_src.concat(res.tempFilePaths)
                    console.log(res.tempFilePaths)
                    // 限制最多只能留下3张照片
                    // _this.data.photo_src = images.length <= 3 ? images : images.slice(0, 3)
                    _this.data.photo_src = res.tempFilePaths
                }
            })
            
            console.log(this.data.photo_src)
        },
        chose_photo: function (evt) {
            let _this = this;
            wx.showActionSheet({
                itemList: ['拍照', '选择本地文件'],
                success(res) {
                    if (res.tapIndex == 0) {
                        wx.chooseImage({
                            count: 6,
                            sizeType: ['original', 'compressed'],
                            sourceType: ['camera'],
                            success(res) {
                                _this.setData({
                                    photo_src: res.tempFilePaths
                                })
                            }
                        })
                    }
                    if (res.tapIndex == 1) {
                        wx.chooseImage({
                            count: 6,
                            sizeType: ['original', 'compressed'],
                            sourceType: ['album'],
                            success(res) {
                                _this.setData({
                                    photo_src: res.tempFilePaths
                                })
                            }
                        })
                    }
                }
            })
        },
        reselect: function (evt) {
            let _this = this
            wx.showActionSheet({
                itemList: ['拍照', '选择本地文件'],
                success(res) {
                    if (res.tapIndex == 0) {
                        wx.chooseImage({
                            count: 6,
                            sizeType: ['original', 'compressed'],
                            sourceType: ['camera'],
                            success(res) {
                                _this.setData({
                                    photo_src: res.tempFilePaths
                                })
                            }
                        })
                    }
                    if (res.tapIndex == 1) {
                        wx.chooseImage({
                            count: 6,
                            sizeType: ['original', 'compressed'],
                            sourceType: ['album'],
                            success(res) {
                                _this.setData({
                                    photo_src: res.tempFilePaths
                                })

                            }
                        })
                    }
                }
            })
        },
        photo_preview: function (evt) {
            let index = evt.target.dataset.idx
            let images = this.data.photo_src
            wx.previewImage({
                current: images[index],
                urls: images
            })
        }
    }
})